package com.android.sreader;

import java.util.ArrayList;
import java.util.List;

import com.android.sreader.PageScrollLayout.OnMiddleClick;
import com.android.sreader.PageScrollLayout.OnPageChange;
import com.android.sreader.format.EpubFile;
import com.android.sreader.utility.Book;
import com.android.sreader.utility.Library;
import com.android.sreader.utility.Paragraph;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Layout;
import android.text.TextPaint;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SReaderBookViewActivity extends Activity {

	private final static String TAG = SReaderApplication.LogBookView;
	private Book book = null;
	private PageScrollLayout pageScrollLayout = null;
	public static final int DIALOG_PROGRESS = 1;

	public static final int cant_load_book = 0;
	
	private Panel panel;
	private ImageButton returnButton;
	private ImageButton chapterButton;
	private TextView pageView;
	private LinearLayout leftLayout;
	private RelativeLayout topLayout;
	private boolean isVisibility = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.bookview);
		this.pageScrollLayout = (PageScrollLayout) this.findViewById(R.id.pageScroll);

		Library l = new Library();
		int pos = this.getIntent().getIntExtra("BookPosition", -1);
		book = l.books.get(pos);
		if (book == null)
			finish();
		
		pageScrollLayout.setBookResource(book);
		panel = (Panel) this.findViewById(R.id.leftPanel);
		returnButton = (ImageButton) this.findViewById(R.id.return_buttom);
		returnButton.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				finish();
			}
		});
		
		chapterButton = (ImageButton) this.findViewById(R.id.chapter_button);
		chapterButton.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				List<String> chapters = book.getChapterNames();
				Intent intent = new Intent();
				intent.setClass(SReaderBookViewActivity.this, ChapterListActivity.class);
				String[] toBeStored = chapters.toArray(new String[chapters.size()]);  
				intent.putExtra("chapters", toBeStored);
				startActivityForResult(intent, 20);
			}
			
		});
		
		pageView = (TextView) this.findViewById(R.id.page_info);
		pageView.setText(Integer.toString(pageScrollLayout.getCurPage()+1));
		
		pageScrollLayout.setOnPageChange(new OnPageChange(){
			public void onPage(int page) {
				// TODO Auto-generated method stub
				pageView.setText(Integer.toString(page));
			}
			
		});
		
		this.topLayout = (RelativeLayout) this.findViewById(R.id.top_layout);
		this.leftLayout = (LinearLayout) this.findViewById(R.id.left_layout);
		leftLayout.setVisibility(View.INVISIBLE);
		topLayout.setVisibility(View.INVISIBLE);
		isVisibility = false;
		
		pageScrollLayout.setOnMiddleClick(new OnMiddleClick(){

			public void onMinddleClick() {
				if(isVisibility == true){
					leftLayout.setVisibility(View.INVISIBLE);
					topLayout.setVisibility(View.INVISIBLE);
					isVisibility = false;
				}else{
					leftLayout.setVisibility(View.VISIBLE);
					topLayout.setVisibility(View.VISIBLE);
					isVisibility = true;
				}
			}
			
		});
		
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private final int MenuBase = Menu.FIRST;
	private final int Library = Menu.FIRST;
	private final int Black = Menu.FIRST + 1;
	private final int NetLibrary = Menu.FIRST + 2;
	private final int Setting = Menu.FIRST + 3;
	private final int Search = Menu.FIRST + 4;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(MenuBase, Library, Library, R.string.library);
		menu.add(MenuBase, Black, Black, R.string.black);
		menu.add(MenuBase, NetLibrary, NetLibrary, R.string.netlibrary);
		menu.add(MenuBase, Setting, Setting, R.string.setting);
		menu.add(MenuBase, Search, Search, R.string.search);
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_PROGRESS:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage("请稍候。。。");
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			return dialog;
		default:
			return null;
		}
	}
	
	@Override
	public void onStop(){
		book.setCurPage(this.pageScrollLayout.getCurPage());
		book.setCurChapter(this.pageScrollLayout.getCurChapter());
		super.onStop();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode,Intent data){
				switch (resultCode){
					case RESULT_OK:
						int num = data.getIntExtra("chapter_num", -1);
						if(num != -1){
							pageScrollLayout.jumpToChapter(num,0);
						}
						break;
				}
	}

}
